/*zhaoyihang
 * 连接sql
 * */

#include "48func.h"

int main()
{
    //创建子线程前，主线程先连接上sql
    MYSQL *mysql;
    //声明sql的锁
    pthread_mutex_t mysql_mutex;
    //初始化锁
    pthread_mutex_init(&mysql_mutex, NULL);
    //初始化sql
    mysql = mysql_init(NULL);
    if (mysql == NULL) {
        fprintf(stderr, "mysql_init() failed\n");
        exit(1);
    }
    //建立mysql链接
    MYSQL * ret = mysql_real_connect(mysql,"localhost","root","123456","MyLuminaryDrive",0,NULL,0);
    if(ret == NULL){
        fprintf(stderr,"mysql connect error:%s\n", mysql_error(mysql));
        exit(1);
    }
    char *userName = "zhao";
    char bufForCheckName[1024]="SELECT * FROM UserTable where username=userName";
    if (mysql_query(mysql, bufForCheckName)) {
        fprintf(stderr, "mysql_query() failed: %s\n", mysql_error(mysql));
        mysql_close(mysql);
        exit(1);
    }

    MYSQL_RES* result = mysql_store_result(mysql);
    if (result == NULL) {
        fprintf(stderr, "mysql_store_result() failed: %s\n", mysql_error(mysql));
        mysql_close(mysql);
        exit(1);
    }
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result)) != NULL) {
        printf("%s\t%s\t%s\t%s\t%s\n", row[0], row[1], row[2],row[3],row[4]);
    }

    mysql_free_result(result);
    mysql_close(mysql);
    exit(0);





}
